package com.bwie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.pojo.User;
import com.bwie.service.UserService;
import com.bwie.mapper.UserMapper;
import com.bwie.utils.R;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

/**
 * @author 24026
 * @description 针对表【t_user】的数据库操作Service实现
 * @createDate 2025-08-28 09:16:50
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
        implements UserService {

    @Autowired
    UserMapper userMapper;
    @Autowired
    StringRedisTemplate redisTemplate;

    @Override
    public R userLogin(User user) {

        User user1 = userMapper.selectOne(new QueryWrapper<User>().lambda().eq(User::getUserPhone, user.getUserPhone()));
        if(user1 == null){
            return R.ERROR(500,"用户未注册");
        }
        String token = Jwts.builder().signWith(SignatureAlgorithm.HS256, "123456").claim("userId", user1.getUserId()).compact();
        if (token == null) {
            return R.ERROR(500, "请前登录");
        }
        String redisToken = "userId" + user1.getUserId();
        redisTemplate.opsForValue().set(redisToken, token, 5, TimeUnit.MINUTES);
        user1.setToken(token);
        return R.OK(user1);
    }
}




